package net.suncaper.demo.controller;

import net.suncaper.demo.domain.Evaluate;
import net.suncaper.demo.domain.Product;
import net.suncaper.demo.domain.User;
import net.suncaper.demo.service.EvaluateService;
import net.suncaper.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("")
public class EvaluateController {
    @Autowired
    private EvaluateService evaluateService;

    @Autowired
    private UserService userService;

//    //评价
//    @GetMapping("/evaluate")
//    public String evaluatePage(Model model){
//        model.addAttribute("evaluate",new Evaluate());
////        System.out.println("test");
//        return "/pages/product/evaluate.html";
//    }

    //评价
    @GetMapping("/evaluate")
    public String evaluatePage(Model model,int userId,int orderId,int productId){
        model.addAttribute("evaluate",new Evaluate(userId,orderId,productId));
//        System.out.println("test");
        return "/pages/product/evaluate.html";
    }


    @PostMapping("/evaluate")
    public String evaluateProduct(Evaluate evaluate){
        evaluateService.saveEvaluate(evaluate);
        int id = evaluate.getUserId();
        User user = userService.finduserById(id);
        int point = 0;
        if (user.getUserPoints()==null){
            point=0;
        }
        else{
            point = user.getUserPoints();
        }
        point+=5;
        user.setUserPoints(point);
        userService.updateUserInfo(user,id);
        return "redirect:/user/userpage";
    }

    @GetMapping("/evaluateAll")
    public String evaluatePage(int userId, Model model){
        List<Evaluate> evaluates=evaluateService.findevaluateByUid(userId);
        model.addAttribute("evaluates",evaluates);
        return "pages/product/evaluateAll.html";
    }
}
